Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Reglas de Firestore | Permitir obtener documentos solo si se proporcionan ID de documentos

Estoy obteniendo mis documentos basados en una lista de identificaciones.

 db.collection("fruits").where(db.FieldPath.documentId(), "in", fruitIds).get()

¿Cómo debo escribir mis reglas de seguridad para permitir la llamada anterior y denegar la llamada a continuación?

 db.collection("fruits").get()
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

No es posible exactamente como usted requiere. Lo que puedes hacer es establecer tus reglas de esta manera:

 match /fruits/{id} { allow get: true; allow list: false; }

Esto permite que los clientes obtengan un documento si conocen la identificación, pero imposibilita la consulta masiva de documentos.

Luego, deberá codificar la solicitud de su aplicación cliente para cada documento individualmente con un DocumentReference get() (en lugar de una consulta con una cláusula where). El impacto en el rendimiento de esto es insignificante (no, no hay ninguna ganancia de rendimiento notable por usar una consulta "en" de la manera que se muestra aquí, y de todos modos está limitado a 10 documentos por lote).

about 4 years ago · Juan Pablo Isaza Report

0

Como @Doug cubrió en su respuesta , esto no se admite actualmente de la manera que espera.

Sin embargo, al mirar la referencia , puede al menos limitar las operaciones de list (consulta) colocando condiciones en orderBy y el limit utilizado por cualquier consulta para hacerlo más difícil en lugar de bloquearlo por completo.

Considere esta respuesta educativa, simplemente busque los elementos uno por uno y deshabilite el acceso a la lista/consulta en sus reglas de seguridad. Se incluye aquí para aquellos que simplemente quieren ofuscar en lugar de bloquear directamente tales consultas.

Esto significaría cambiar su consulta a:

 db.collection("fruits") .where(db.FieldPath.documentId(), "in", fruitIds) .orderBy(db.FieldPath.documentId()) // probably implicitly added by the where() above, but put here for good measure .limit(10) // this limit applies to `in` operations anyway, but for this to work needs to be added .get()
 service cloud.firestore { match /databases/{database}/documents { // Matches any document in the cities collection as well as any document // in a subcollection. match /fruits/{fruit} { allow read: if <condition> // Limit documents per request to 10 and only if they provide an orderBy clause allow list: if <condition> && request.query.limit <= 10 && request.query.orderBy = "__name asc" // __name is FieldPath.documentId() allow write: if <condition>; } } }

Usando esas restricciones, esto ya no debería funcionar:

 db.collection("fruits").get()

Pero aún podría raspar todo en trozos más pequeños usando:

 const fruits = []; const baseQuery = db.collection("fruits") .orderBy(db.FieldPath.documentId()) .limit(10); while (true) { const snapshot = await (fruits.length > 0 ? baseQuery.startAt(fruits[fruits.length-1]).get() : baseQuery.get()) Array.prototype.push.apply(fruits, snapshot.docs); if (snapshot.empty) { break; } } // here, fruits now contains all snapshots in the collection
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!